package cn.wuyu.baselibrary.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import cn.wuyu.baselibrary.R;
/**
 * author : xuzongmeng
 * date   : 2019/4/2.
 * desc   : 描述
 * http://test.wuyuqizhu.com/webapp/#/
 */
public class WebViewActivity extends BaseActivity {
    private Bundle mBundle;
    public static final String WEB_URL = "loading_URL";//要加载的页面
    public static final String WEB_TITLE = "URL_title";//标题
    public static final String WEB_TEXT = "URL_text";//
    private String title = "";
    private String text = "";
    FrameLayout flContainer;
    private String url;
    private WebViewFragment webViewFragment;

    public static void openActivity(Activity activity, String url, String title, String text) {
        Intent intent = new Intent(activity, WebViewActivity.class);
        intent.putExtra(WEB_URL, url);
        intent.putExtra(WEB_TITLE, title);
        intent.putExtra(WEB_TEXT, text);
        activity.startActivity(intent);
    }

    public static void openActivity(Activity activity, String url, String title) {
        Intent intent = new Intent(activity, WebViewActivity.class);
        intent.putExtra(WEB_URL, url);
        intent.putExtra(WEB_TITLE, title);
        activity.startActivity(intent);
    }
    @Override
    protected int getLayoutID() {
        return R.layout.activity_webview;
    }

    @Override
    public String getToolBarTitle() {
        return getIntent().getExtras().getString(WEB_TITLE);
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        init();
    }

//
//    @Override
//    public void onCreateCustomToolBar(Toolbar toolbar) {
//        toolbar.inflateMenu(R.menu.shard_menu);
//        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
//            @Override
//            public boolean onMenuItemClick(MenuItem item) {
//                switch (item.getItemId()) {
//                    case R.id.menu_shard:
//                        //业务逻辑
//                        break;
//                }
//                return true;
//            }
//        });
//    }


    private void init() {
        mBundle = getIntent().getExtras();
        if (mBundle != null) {
            url = mBundle.getString(WEB_URL);
            title = mBundle.getString(WEB_TITLE);
            text = mBundle.getString(WEB_TEXT);
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            webViewFragment = WebViewFragment.newInstance(url);
            ft.add(R.id.flContainer, webViewFragment);
            // 事务提交
            ft.commit();
        }
    }
//    @Override
//    protected boolean onPrepareOptionsPanel(View view, Menu menu) {
//        if (menu != null) {
//            if (menu.getClass() == MenuBuilder.class) {
//                try {
//                    Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
//                    m.setAccessible(true);
//                    m.invoke(menu, true);
//                } catch (Exception e) {
//                    LogUtil.e("onMenuOpened...unable to set icons for overflow menu" + e);
//                }
//            }
//        }
//        return super.onPrepareOptionsPanel(view, menu);
//    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webViewFragment.getWebview().canGoBack()) {
            webViewFragment.getWebview().goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
